﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace RR{
    public class CubeCtrl : MonoBehaviour
    {
        //设置bool判断方块位置
        public bool isLeftCube;
        //cube的移动速度
        public float moveSpeed;
        //显示gameover的label
        public Text gameoverText;
        //显示最高成绩的面板
        public Text bestScore;
        //分数面板
        public Text scoreLab;
        public Button playAgain;
        private float timeNow;
        //根据分数变化更改plane颜色
        public GameObject[] plane;

        private Vector3 m_leftPostion;
        private Vector3 m_rightPosition;

        private Vector3 n_leftPostion;
        private Vector3 n_rightPosition;

        public Color[] color_left;
        public Color[] color_right;

        //设置lerp函数缓慢变色的计时器
        private static float startTime;
        private float intervalTime;
        private static bool isLerp = false;

        //设置gameobject更改player的texture
        public GameObject[] playerTexture;

        // Use this for initialization
        void Start()
        {
            m_leftPostion = new Vector3(3.76f, 0.6f, -10f);
            m_rightPosition = new Vector3(1.17f, 0.6f, -10f);

            n_leftPostion = new Vector3(-1.17f, 0.6f, -10f);
            n_rightPosition = new Vector3(-3.76f, 0.6f, -10f);
        }

        public void InitGameScene()
        {
            timeNow = 0;
            Global.scoreNow = 0;
            Global.isGameOver = false;
            SceneManager.LoadScene("gameScene");
            if (isLeftCube)
            {
                transform.position = m_leftPostion;
            }
            else
            {
                transform.position = new Vector3(-1.17f, 0.6f, -20f);
            }

        }

        private void OnCollisionEnter(Collision collision)
        {
            if (collision.gameObject.tag == "Player")
            {
                Global.SetMaxScore(Global.scoreNow);
                Global.GetMaxScore();
                Global.GameOver();
                Global.lastValue = 5;
                gameoverText.gameObject.SetActive(true);
                bestScore.gameObject.SetActive(true);
                playAgain.gameObject.SetActive(true);
                Debug.Log("MaxScore == " + Global.MaxScore);

                bestScore.text += Global.MaxScore;
            }

        }

        private void UpdateCubePos()
        {
            int randomNum_pos = Random.Range(0, 2);
            int randomNum_scale = Random.Range(2, 8);
            if (randomNum_pos == 1)
            {
                if (isLeftCube)
                {
                    transform.position = m_leftPostion;
                }
                else
                {
                    transform.position = n_leftPostion;
                }
                transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, randomNum_scale);
            }
            else
            {
                if (isLeftCube)
                {
                    transform.position = m_rightPosition;
                }
                else
                {
                    transform.position = n_rightPosition;
                }
                transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, randomNum_scale);
            }
        }

        private void UpdatePosition()
        {
            transform.position += Vector3.forward * moveSpeed * Time.deltaTime;

            if (transform.position.z >= 12f)
            {
                UpdateCubePos();
            }
        }

        private void UpdateScore()
        {
            //计时、分数
            if (Time.timeSinceLevelLoad - timeNow >= 1f && isLeftCube)
            {
                timeNow = Mathf.Floor(Time.timeSinceLevelLoad);
                scoreLab.text = "" + timeNow;
                Global.scoreNow = (int)timeNow;
                //Debug.Log("scoreNow == " + Global.scoreNow);

                int index = Random.Range(0, 10);
                if (timeNow % 10 == 0 && isLeftCube)
                {
                    // Debug.Log("isleft");
                    //this.GetComponent<Renderer>().material.color = color_right[index];
                    //plane[1].GetComponent<Renderer>().material.color = color_right[index];
                    //Global.isColor = true;
                    //Global.value = index;

                    //this.GetComponent<Renderer>().material.color = color_right[index];
                    //plane[1].GetComponent<Renderer>().material.color = color_right[index];
                    Global.isColor = true;
                    Global.value = index;

                    startTime = Time.time;
                    intervalTime = 0;
                    isLerp = true;


                    //this.GetComponent<Renderer>().material.color = color_right[0];
                    //plane[1].GetComponent<Renderer>().material.color = color_right[0];
                    //Global.isColor = true;
                    //Global.value = 0;
                }
            }
        }

        private void UpdateRightColor()
        {
            if (Time.time - startTime <= 4f)
            {
                intervalTime = (Time.time - startTime) / 4f;
                this.GetComponent<Renderer>().material.color = Color.Lerp(color_right[Global.lastValue], color_right[Global.value], intervalTime);
                plane[1].GetComponent<Renderer>().material.color = Color.Lerp(color_right[Global.lastValue], color_right[Global.value], intervalTime);
            }
            else
            {
                isLerp = false;
                this.GetComponent<Renderer>().material.color = Color.Lerp(color_right[Global.lastValue], color_right[Global.value], 1.0f);
                plane[1].GetComponent<Renderer>().material.color = Color.Lerp(color_right[Global.lastValue], color_right[Global.value], 1.0f);

                Global.lastValue = Global.value;
                Debug.Log("islerp close");
                startTime = 0;
                intervalTime = 0;
            }
        }

        private void UpdateLeftColor()
        {
            //int value = Global.value;
            //this.GetComponent<Renderer>().material.color = color_left[value];
            //plane[0].GetComponent<Renderer>().material.color = color_left[value];
            if (Time.time - startTime <= 4f)
            {
                intervalTime = (Time.time - startTime) / 4f;
                this.GetComponent<Renderer>().material.color = Color.Lerp(color_left[Global.lastValue], color_left[Global.value], intervalTime);
                plane[0].GetComponent<Renderer>().material.color = Color.Lerp(color_left[Global.lastValue], color_left[Global.value], intervalTime);
            }
            else
            {
                this.GetComponent<Renderer>().material.color = Color.Lerp(color_left[Global.lastValue], color_left[Global.value], 1.0f);
                plane[0].GetComponent<Renderer>().material.color = Color.Lerp(color_left[Global.lastValue], color_left[Global.value], 1.0f);
                playerTexture[1].GetComponent<PlayerCtrl>().InitTexture(Global.value);
                playerTexture[0].GetComponent<PlayerCtrl>().InitTexture(Global.value);
                Debug.Log("left color islerp close");
                startTime = 0;
                intervalTime = 0;
                Global.isColor = false;
            }
        }

        private void FixedUpdate()
        {
            if (!Global.isGameOver)
            {
                UpdatePosition();
                UpdateScore();
            }

            if (isLerp)
            {
                UpdateRightColor();
            }

            if (!isLeftCube && Global.isColor)
            {
                UpdateLeftColor();

            }
        }
    }
}

